package com.example.demo.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * @program: demo
 * @author: JCM
 * @create: 2023-03-29 14:20
 * @description:
 **/
@Component // 随着框架的启动而启动
@Aspect    // 告诉框架这是一个切面
public class UserAspect {

    /**
     * 切点
     */
    @Pointcut("execution(* com.example.demo.controller.UserController.*(..))")
    public void pointcut() {

    }

    // 前置通知
    @Before("pointcut()")
    public void beforeAdvice() {
        System.out.println("执行了前置通知");
    }
    //  后置通知
    @After("pointcut()")
    public void afterAdvice() {
        System.out.println("执行了后置通知");
    }

    @Around("pointcut()")
    public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("开始执行环绕通知");
        Object obj = null;
        // 执行目标方法
        obj = joinPoint.proceed();
        System.out.println("退出环绕通知");
        return obj;
    }
}
